package com.yuanfeng.job.job;


import com.yuanfeng.job.dto.AppJiPushOrderDTO;
import com.yuanfeng.job.service.UpdateOrderStatusService;
import com.yuanfeng.job.service.UpdateRecordStatusService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UpdateReceiverOrderStatusJob implements BaseJob {

	private static final Logger logger = LoggerFactory.getLogger(UpdateReceiverOrderStatusJob.class);

	@Autowired
	UpdateOrderStatusService updateOrderstatusService;
	
	@Autowired
    UpdateRecordStatusService updateRecordStatusService;

	public UpdateReceiverOrderStatusJob() {

	}

	//更新已收货 ，但是超过时间未确认收货订单
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		//KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
		//获取待更新状态订单的 paymentNumber
		List<AppJiPushOrderDTO> orderIds = updateOrderstatusService.listForUpdateOrder();

        //更新所有待超时订单
		Integer countOrder =0;

		for(AppJiPushOrderDTO orderId:orderIds){
			logger.info("更新所有超时未确认收货订单相关记录，订单号为：" + orderId.getOrderId());
			 countOrder += updateOrderstatusService.updateForUpdateOrder(orderId.getOrderId());
		}
		logger.info("更新所有超时未确认收货订单，更新数量为：" + countOrder);
		
		//更新所有待超时订单(orderGoods表)
		for(AppJiPushOrderDTO orderId:orderIds){
			updateOrderstatusService.updateForUpdateGoods(orderId.getOrderId());
		}
		
		
		//更新所有超时订单相关的记录	
		//当trade_type_id = 1，即为购物时再更新
		Integer countRecord = 0;
		for(AppJiPushOrderDTO orderId:orderIds){
			countRecord += updateRecordStatusService.updateReceiverdRecord(orderId.getOrderId());
		}
		logger.info("更新所有超时未确认收货订单相关记录，更新数量为：" + countRecord);

		logger.info("更新所有超时未确认收货订单相关Trade");
		for(AppJiPushOrderDTO orderId:orderIds){
			countRecord += updateRecordStatusService.updateReceiverdTrade(orderId.getOrderId());
		}
	}

}
